package com.qin.EducationalManger.student.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Caret;

import org.json.JSONObject;

import com.qin.EducationalManger.student.model.StudentModel;
import com.qin.EducationalManger.student.service.impl.StudentServiceImpl;





/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

 

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		
		 Object res ="";
		 request.setCharacterEncoding("UTF-8");
		 response.setCharacterEncoding("UTF-8");
		  
	
		 System.out.println(request.getParameter("action"));
	    switch (request.getParameter("action")) {
//		case "查询":
//			res= select(request);
//		   
//			
//			response.getWriter().write(new JSONArray((Collection<StudentModel>) res).toString());
//			break;
		case "删除":
			String de = delete(request)+"";
			
			response.getWriter().write(de);
			
			break;
		case "添加":
			String ree=  add(request)+"";
			System.out.println("case:  "+ree);  //测试 返回数据
			response.getWriter().write(ree);
			break;
		case "修改":
		   
		String upr = update(request)+"";
		System.out.println(upr);
		  response.getWriter().write(upr);  
		   break;
		case"查询":
			Map<String, Object> rec = page(request);
		
			response.getWriter().write(new JSONObject(rec).toString());
			
			break;
			
		case"删除课程信息":
		
			 String red = deleteCourse(request).toString();
			 response.getWriter().write(red);
			
			break;
		
			
		default:
			break;
		}
		
	

	}		
	

		//删除单一课程
	private Object deleteCourse(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String studentId =request.getParameter("studentId");
		String courseId =request.getParameter("courseId");
		

		return  service.updataCourse(studentId,courseId);
	}

	private Map<String, Object> page(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String studentId = request.getParameter("studentId");	
		String studentName = request.getParameter("studentName");
		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");
		
		StudentModel model = new StudentModel();
		model.setStudentId(studentId);
		model.setStudentName(studentName);
		
		if(pageIndex==null) {
			return null;
		}
		
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<StudentModel> models = service.selectAll(model);
		
		
		int count = service.selectCount(model);
		
		
	
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", models);
		map.put("count", count);
		
		return map;
		
		
	
	}

	private Integer update(HttpServletRequest request) {
		// TODO Auto-generated method stub
	
	
		
		String studentId= request.getParameter("studentId");
		
	
		String studentName = request.getParameter("studentName");
   
		String age_ = request.getParameter("age");
		
		Integer age=null;  //避免空指针异常
		if(age_!=null&&age_.intern().isEmpty()==false) {
			age= Integer.parseInt(age_);
		}
		
		System.out.println("age"+age_);
		String studentSex = request.getParameter("studentSex");
		String courseId = request.getParameter("courseId");
	
		System.out.println("courseId"+courseId);
		
		StudentModel  model = new StudentModel(studentId,studentName,age,studentSex,courseId);
		
		
	 return	service.update(model);
		
		
	}
	private StudentServiceImpl service = new StudentServiceImpl();
//添加
	    private int add(HttpServletRequest request) {
		// TODO Auto-generated method stub
	    	StudentModel model = new StudentModel();
	    	String studentId =request.getParameter("studentId");
	        model.setStudentId(studentId);
	    
	    	List<StudentModel> models = service.selectAll(model);//先查询数据库中是否已经存在该学号
	    	
	    	
	    	
	    	if(models.isEmpty()) {  //如果不存在，再往数据库插入
	        		
	    	String studentName =request.getParameter("studentName");
	    	String _age = request.getParameter("age");
	    	Integer age = null ;
	    	if(_age!=null&&_age.intern().isEmpty()==false) {   //避免年龄为空  导致 空指针异常
	    		age=Integer.parseInt(_age);
	    	}
	    	
	    	String studentSex =request.getParameter("studentSex");
	    	
	    	String []courseIds =request.getParameterValues("courseId");
	 	   
	    	String courseId ="";
	    	for(String i:courseIds) {
	    		System.out.println("                            "+i);
	    		courseId+=i+" ";
	    	}
	    	
	    	
	    	model.setStudent_sex(studentSex);
	    	model.setAge(age);
	    	model.setCourseId(courseId);
	    	model.setStudentName(studentName);
	    	
	    	
	    	return service.insert(model);
	    	}else {
				return 0;
			}
	    	
		
	}

		private int delete(HttpServletRequest request) {
		// TODO Auto-generated method stub
	    	String studentId = request.getParameter("studentId");
	    	
	    	
	    	
	    	StudentModel model =  new StudentModel();  
	    	
	    	model.setStudentId(studentId);
	    	
	    	return service.delete(model);
	    	
	    	
	    	
	
	}


	private List<StudentModel> select(HttpServletRequest request) {
		
		String studentId = request.getParameter("studentId");
		String studentName = request.getParameter("studentName");
		
		
		
		StudentModel model = new StudentModel();
		
		
		
		List<StudentModel> models = service.selectAll(model);
	
		
		return models;
		// TODO Auto-generated method stub
		
		
	}
}